/**
 * 
 */
package com.nickstumpos.android.GmailWidgetPlus.labels;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.nickstumpos.android.GmailWidgetPlus.R;
import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;
import com.nickstumpos.android.GmailWidgetPlus.gmailProvider.GmailProvider;

/**
 * @author O254802
 * 
 */
public class LabelBlackListManager {

	private static Map<Integer, List<String>>	blacklists	= null;

	public static boolean blacklist(final String label, final int widgetId) {

		boolean ret = false;
		if (LabelBlackListManager.isInit(widgetId)) {
			ret = LabelBlackListManager.blacklists.get(widgetId).add(label);
		}
		return ret;
	}

	public static void
			commitBlacklist(final Context context, final int widgetId) {

		if (LabelBlackListManager.isInit(widgetId)) {
			final SharedPreferences.Editor prefsEditor = context.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF,
					0)
					.edit();
			final Base64 encoder = new Base64();
			final ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream out = null;
			try {
				out = new ObjectOutputStream(bos);
				out.writeObject(LabelBlackListManager.blacklists.get(widgetId));
				out.close();
				final byte[] byteParameter = encoder.encode(bos.toByteArray());
				String serialParameter = new String(byteParameter);
				serialParameter = serialParameter.replace("\n", "");
				serialParameter = serialParameter.replace("\r", "");
				prefsEditor.putString(GMWP_Constants.PREF_LABEL_BLKLST_REF + widgetId,
						serialParameter);
				prefsEditor.commit();
			} catch (final Exception e) {
				Toast.makeText(context,
						context.getString(R.string.saving_blacklist_failed),
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public static void initBlacklist(final Context context, final int widgetId) {

		if (!LabelBlackListManager.isInit(widgetId) && (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID)) {
			if (LabelBlackListManager.blacklists == null) {
				LabelBlackListManager.blacklists = new HashMap<Integer, List<String>>();
			}
			final SharedPreferences prefs = context.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF,
					0);
			final String blacklistBytes = prefs.getString(GMWP_Constants.PREF_LABEL_BLKLST_REF + widgetId,
					null);
			if (blacklistBytes != null) {
				final byte[] buf = Base64.decodeBase64(blacklistBytes.getBytes());
				ObjectInputStream in = null;
				try {
					in = new ObjectInputStream(new ByteArrayInputStream(buf));
					LabelBlackListManager.blacklists.put(widgetId,
							(List<String>) in.readObject());
				} catch (final Exception e) {
					LabelBlackListManager.blacklists.put(widgetId,
							new ArrayList<String>());
				}
			} else {
				LabelBlackListManager.blacklists.put(widgetId,
						new ArrayList<String>());
			}
			if (LabelBlackListManager.blacklists.get(widgetId).size() < 1) {
				String name = prefs.getString(GMWP_Constants.PREF_ACCOUNT_REF + widgetId,
						null);
				if (name == null) {
					final Account[] accounts = AccountManager.get(context)
							.getAccountsByType(GMWP_Constants.GOOGLE_ACCOUNT_TYPE);
					if ((accounts != null) && (accounts[0] != null)) {
						name = accounts[0].name;
					}
				}
				if (name != null) {
					final List<String> labelNames = new GmailProvider(context,
							name,
							false,
							null).getLabelsNames();
					for (final String s : labelNames) {
						SystemLabels sl = null;
						if (s.startsWith(GMWP_Constants.SYSTEM_LABEL_PREFIX)) {
							sl = SystemLabels.systemLabelByCode(s);
						}
						if ((sl != null) && sl.isBlacklistByDefault()) {
							LabelBlackListManager.blacklists.get(widgetId)
									.add(sl.getCode());
						}
					}
				}
			}
			LabelBlackListManager.commitBlacklist(context, widgetId);
		}
	}

	public static boolean isBlacklisted(final String label, final int widgetId) {

		boolean ret = false;
		if (LabelBlackListManager.isInit(widgetId)) {
			ret = LabelBlackListManager.blacklists.get(widgetId).indexOf(label) >= 0;
		} else {
			if (label.startsWith(GMWP_Constants.SYSTEM_LABEL_PREFIX)) {
				final SystemLabels sl = SystemLabels.systemLabelByCode(label);
				if (sl != null) {
					ret = sl.isBlacklistByDefault();
				} else {
					ret = true;
				}
			}
		}
		return ret;
	}

	public static boolean isInit(final int widgetId) {

		boolean ret = false;
		if ((LabelBlackListManager.blacklists != null) && LabelBlackListManager.blacklists.containsKey(widgetId)) {
			ret = true;
		}
		return ret;
	}
}
